package com.mini.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mini.entity.*;
import com.mini.service.VaccineUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hy
 * @since 2021-04-28
 */
@RestController
@CrossOrigin
@Api(description = "用户预约接种", value = "用户预约接种", tags = "用户预约接种")
@RequestMapping("/mini/vaccineUserInfo")
public class VaccineUserInfoController {
    @Autowired
    private VaccineUserInfoService vaccineUserInfoService;

    @PostMapping()
    @ApiOperation(value = "增")
    public Result save(@RequestBody VaccineUserInfoDto vaccineUserInfoDto) {
        VaccineUserInfo vaccineUserInfo = new VaccineUserInfo();
        BeanUtils.copyProperties(vaccineUserInfoDto, vaccineUserInfo);
        return vaccineUserInfoService.save(vaccineUserInfo) ? Result.success() : Result.error();
    }

    @PutMapping
    @ApiOperation(value = "改")
    public Result update(@RequestBody VaccineUserInfoVo vaccineUserInfoVo) {
        VaccineUserInfo vaccineUserInfo = new VaccineUserInfo();
        BeanUtils.copyProperties(vaccineUserInfoVo, vaccineUserInfo);
        return vaccineUserInfoService.updateById(vaccineUserInfo) ? Result.success() : Result.error();
    }

    @GetMapping("/find/{userId}")
    @ApiOperation(value = "查")
    public Result select(@PathVariable(value = "userId", required = true) String userId) {
        QueryWrapper<VaccineUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        VaccineUserInfo info = vaccineUserInfoService.getOne(wrapper);
        return Result.success().data(info);
    }
    @ApiOperation(value = "分页查询")
    @GetMapping("/find/{current}/{size}/{userId}")
    public Result xixi(@PathVariable(value = "current", required = true) Integer current,
                       @PathVariable(value = "size", required = true) Integer size,
                       @PathVariable(value = "userId", required = true) String userId) {
        QueryWrapper<VaccineUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        Page<VaccineUserInfo> page = new Page<>(current, size);
        vaccineUserInfoService.page(page, wrapper);
        return Result.success().data(page);
    }

    @DeleteMapping("/del/{userId}")
    @ApiOperation(value = "删")
    public Result delete(@PathVariable(value = "userId", required = true) String userId) {
        return vaccineUserInfoService.removeById(userId) ? Result.success() : Result.error();
    }

}

